package main.controller.admin;

import main.RequestHelper;
import main.TimeHelper;
import main.database.AccountHelper;
import main.database.ArticleHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
public class FictionUpdatePageController {
    @RequestMapping(value = "/fictionupdate")
    public String fictionUpate(HttpServletRequest request, @RequestParam String id, ModelMap modelMap) {
        String uuid = RequestHelper.getUserUUIDFromRequest(request);
        boolean isAdmin = AccountHelper.isAdminFromUUID(uuid);
        String account = AccountHelper.getAccountInfoFromUUID(uuid);

        String url = "admin/fictionUpdatePage";
        if (isAdmin) {
            String[] fields = ArticleHelper.getFieldsFromId(id);
            if (fields != null) {
                boolean finished = false;

                if (fields[6].equals("true")) {
                    finished = true;
                }

                modelMap.put("fictionid", id);
                modelMap.put("title", fields[0]);
                modelMap.put("description", fields[1]);
                modelMap.put("rootcatlog", fields[2]);
                modelMap.put("catlog", fields[3]);
                modelMap.put("finishyear", fields[4]);
                modelMap.put("finished", finished);
                modelMap.put("author", fields[5]);
                modelMap.put("userName", account);
                modelMap.put("year2year", TimeHelper.getFormattedYear2Year());
            }
            else {
                //TODO: no error diretion now
                url = "/error";
            }
        }
        else {
            url = "/admin";
        }

        return url;
    }
}
